/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.xml;

/**
 * Represents a PUBLIC document type. The subclasses have to specify the name of the root element, the id of document
 * type and the URL of it.
 * @author Peter Bona
 */
public abstract class PublicDocType extends PrintableXMLMarkup implements DocType {

	/**
	 * Delegates the call to {@link XMLVisitor#publicDocType(CharSequence, CharSequence, CharSequence)}
	 */
	public final void visit(XMLVisitor xmlProcessor) throws XMLVisitingException {
		if (xmlProcessor != null) {
			xmlProcessor.publicDocType(getRootTagName(), getId(), getURL());
		}
	}

	/**
	 * @return the name of the root element
	 */
	protected abstract String getRootTagName();

	/**
	 * @return the id of document type
	 */
	protected abstract String getId();

	/**
	 * @return the URL of the document type
	 */
	protected abstract String getURL();

	/**
	 * Implemented hash code based on root tag's name, the id and URI
	 */
	@Override
	public final int hashCode() {
		return getRootTagName().hashCode() + getId().hashCode() + getURL().hashCode();
	}

	/**
	 * Implemented based on the hash code
	 * @param value
	 * @return true if the specified value is of type DocType and the hash codes equal.
	 */
	@Override
	public final boolean equals(Object value) {
		if (!(value instanceof DocType)) {
			return false;
		}

		DocType docType = (DocType) value;

		return docType.hashCode() == hashCode();
	}

}
